package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import model.Tpermission;
import model.Trole;
import model.Trolepermission;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.PermissionDaoI;
import dao.RoleDaoI;
import dao.RolePermissionDaoI;
import pageModel.DataGrid;
import pageModel.Role;
import service.RoleServiceI;

@Service("roleService")
public class RoleServiceImpl implements RoleServiceI {
private RoleDaoI roleDao;
private RolePermissionDaoI rolePermissionDao;
private PermissionDaoI permissionDao;
	public RoleDaoI getRoleDao() {
	return roleDao;
}
@Autowired
public void setRoleDao(RoleDaoI roleDao) {
	this.roleDao = roleDao;
}

	public RolePermissionDaoI getRolePermissionDao() {
	return rolePermissionDao;
}
	@Autowired
public void setRolePermissionDao(RolePermissionDaoI rolePermissionDao) {
	this.rolePermissionDao = rolePermissionDao;
}	
	public PermissionDaoI getPermissionDao() {
		return permissionDao;
	}
	@Autowired
	public void setPermissionDao(PermissionDaoI permissionDao) {
		this.permissionDao = permissionDao;
	}
	@Override
	public DataGrid datagrid(Role role) {
		DataGrid dg=new DataGrid();
		String hql="from Trole  t where 1=1";
		String totalHql="select count(*) "+hql;
		if(role.getSort()!=null){
		hql+=" order by "+role.getSort()+" "+role.getOrder();
		}
		List< Trole> su=roleDao.find(hql,role.getPage(),role.getRows());
		List<Role> l=new ArrayList<Role>();
		if(su!=null&&su.size()>0){
			for(  Trole t :su){
				Role r=new Role();
				BeanUtils.copyProperties(t, r);
				l.add(r);
			}
		}
		dg.setTotal(roleDao.count(totalHql));
		dg.setRows(l);
		return dg;
	}

	@Override
	public void remove(String ids) {
		String [] nids=ids.split(",");
		String hql="delete Trole  t where t.id in(";
		for(int i=0;i<nids.length;i++){
			if(i>0){
				hql+=",";
			}
			hql+="'"+nids[i]+"'";
		}
		hql+=")";
		roleDao.executeHql(hql);

	}

	@Override
	public void edit(Role role) {
		 Trole t=roleDao.get( Trole.class, role.getId());
		 BeanUtils.copyProperties(role, t);
	}

	@Override
	public Role save(Role role) {
		   Trole r=new  Trole();
		   BeanUtils.copyProperties(role,r);
		   r.setId(UUID.randomUUID().toString());
		   roleDao.save(r);
		   BeanUtils.copyProperties(r, role);
		   return role;
	}
	public List<Role> combobox() {
		List<Trole> l = roleDao.find("from Trole t");
		List<Role> rl = new ArrayList<Role>();
		if (l != null && l.size() > 0) {
			for (Trole t : l) {
				Role r = new Role();
				BeanUtils.copyProperties(t, r);
				rl.add(r);
			}
		}
		return rl;
	}
	@Override
	public void grant(String id, String ids) {
		String flag = "no";
		String hql2 = "from Trolepermission t where t.trole.id=:roleid";
		Map<String,Object> params2 = new HashMap<String,Object>();
		params2.put("roleid",id);
		List<Trolepermission> lt = rolePermissionDao.find(hql2, params2);
		System.out.println("rolepermission33333"+lt.size());
		for(Trolepermission t : lt){
			for(String resourceId : ids.split(",")){
				if(t.getTpermission().getId().trim().equals(resourceId)){
					flag="yes";
				}
			}
			System.out.println(flag);
			if(flag.trim().equals("no")){
				System.out.println(flag);
				System.out.println("hahahaahah");
				System.out.println(t.getTpermission().getId());
				String hql3="delete Trolepermission  t where t.trole.id=:id and t.tpermission.id=:perid";
				Map<String,Object> params3 = new HashMap<String,Object>();
				params3.put("id",id);
				params3.put("perid",t.getTpermission().getId());
				rolePermissionDao.executeHql(hql3,params3);
				flag = "no";
			}
		}
		Trole t = roleDao.get(Trole.class, id);
		if(t!=null&&ids.length()>0){
		for (String resourceId : ids.split(",")) {
			String hql = "from Trolepermission t where t.trole.id=:id and t.tpermission.id=:pid";
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("id",id);
			params.put("pid",resourceId);
			List<Trolepermission> l = rolePermissionDao.find(hql, params);
			System.out.println("rolepermission22222"+l.size());
			if(l.size()==0){
				Trolepermission tr = new Trolepermission();
				tr.setId(UUID.randomUUID().toString());
				tr.setTrole(t);
				String hql1 ="from Tpermission t where t.id=:id";
				Map<String,Object> params1 = new HashMap<String,Object>();
				params1.put("id",resourceId);
				Tpermission tp = permissionDao.get(hql1, params1);
				tr.setTpermission(tp);
				rolePermissionDao.save(tr);
			}
		}
		}
	}
}
